import 'package:core_package/core_package.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

mixin BaseRefreshDelegate {
  int pageSize = 0;
  bool notMore = false;

  EasyRefreshController controller = EasyRefreshController();

  Future reloadData(int page);

  void endRefresh(bool more) {
    if (more != null && more == false) {
      controller.finishLoadCallBack?.call();
      notMore = true;
    } else {
      notMore = false;
      controller.resetLoadStateCallBack?.call();;
    }
  }

  Widget buildRefreshDelegate(
    BuildContext context, {
    required Widget slier,
  }) {
    return EasyRefresh(
      child: slier,
      header: RefreshBasics.header,
      footer: RefreshBasics.footer,
      controller: controller,
      onRefresh: () async {
        pageSize = 0;
        return reloadData(pageSize);
      },
      onLoad: () async {
        if (notMore) return;
        pageSize++;
        return reloadData(pageSize);
      },
    );
  }
}
